package sisi.invoices;

import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Window;

import sisi.General;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class CustinvoiceWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose {
	// window
	private Window windowCustinvoice;
	private Listbox boxCustinvoice;
	private List lista_Custinvoice;
	private boolean lNuovo;
	private ListModelList listModelList;
	private Integer idSelezionato = 0;

	protected Map<String, Object> args;
	private Object CustinvoiceItem;
	private Custinvoice modCustinvoice;

	public void onCreate$windowCustinvoice(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N"
				: tastoSeleziona);
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Custinvoice = new CustinvoiceController()
				.getList("custnuminvoice");
		listModelList.addAll(lista_Custinvoice);
		boxCustinvoice.setModel(listModelList);
		boxCustinvoice.setItemRenderer(new CustinvoiceItemRenderer());

	}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public class CustinvoiceItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {

			Custinvoice ci = (Custinvoice) data;

			new Listcell("" + ci.getCustid()).setParent(li);
			new Listcell("" + ci.getCustname()).setParent(li);
			new Listcell("" + ci.getOrder()).setParent(li);
			new Listcell("" + ci.getCustnuminvoice()).setParent(li);
			new Listcell("" + General.formatoFecha(ci.getInvoicedt()))
					.setParent(li);
			new Listcell("" + General.formatoFecha(ci.getDeliverydt()))
					.setParent(li);
			new Listcell(""
					+ (ci.getInvoicedt() == null ? "" : ci.getInvoicedt()
							.getYear() + 1900)).setParent(li);
			new Listcell("" + ci.getCurr()).setParent(li);
			new Listcell(""
					+ General.decimalFormat("###,###,##0.00",
							ci.getTotaltaxedvat())).setParent(li);
			li.setAttribute("rigaCustinvoice", data);
		}
	}

}
